﻿@{
    ViewData["Title"] = "Database Migration";
}
@inject IConfigHelper config
@inject ITranslationHelper translator
@{
    var userLanguage = config.GetServerLanguage();
}
@using CarCareTracker.Helper
@section Nav {
    <div class="container-fluid lubelogger-navbar-container frosted hideOnPrint">
        <div class="row mt-2">
            <div class="d-flex lubelogger-navbar">
                <div class="me-2" style="cursor:pointer;" onclick="returnToGarage()">
                    <img src="@config.GetSmallLogoUrl()" class="lubelogger-logo" />
                </div>
                <span class="text-truncate lead">@translator.Translate(userLanguage, "Database Migration")</span>
            </div>
        </div>
        <hr />
    </div>
}
@model AdminViewModel
<div class="container">
    <div class="row">
        <div class="col-12">
            <ul class="list-group list-group-flush">
                <li class="list-group-item">@translator.Translate(userLanguage, "Instructions")</li>
                <li class="list-group-item">@translator.Translate(userLanguage, "Use this tool to migrate data between LiteDB and Postgres")</li>
                <li class="list-group-item">@translator.Translate(userLanguage, "Note that it is recommended that the Postgres DB is empty when importing from LiteDB to prevent primary key errors.")</li>
            </ul>
        </div>
    </div>
    <hr />
    <div class="row d-flex justify-content-center">
        <div class="col-3">
            <div class="d-grid">
                <input onChange="importPostgresData(this)" type="file" accept=".db" class="d-none" id="inputImport">
                <button type="button" class="btn btn-warning mt-2" onclick="importToPostgres()"><i class="bi bi-upload me-2"></i>@translator.Translate(userLanguage, "Import To Postgres")</button>
            </div>
            <div class="d-grid">
                <button type="button" class="btn btn-warning mt-2" onclick="exportFromPostgres()"><i class="bi bi-download me-2"></i>@translator.Translate(userLanguage, "Export From Postgres")</button>
            </div>
        </div>
    </div>
</div>
<script>
    function exportFromPostgres(){
        sloader.show();
        $.get('/Migration/Export', function (data) {
            sloader.hide();
            if (data.success) {
                window.location.href = data.message;
            } else {
                errorToast(genericErrorMessage());
            }
        });
    }
    function importPostgresData(event) {
        let formData = new FormData();
        formData.append("file", event.files[0]);
        sloader.show();
        $.ajax({
            url: "/Files/HandleFileUpload",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            type: 'POST',
            success: function (response) {
                if (response.trim() != '') {
                    $.post('/Migration/Import', { fileName: response }, function (data) {
                        sloader.hide();
                        if (data.success) {
                            successToast(data.message);
                            setTimeout(function () { window.location.href = '/Home/Index' }, 500);
                        } else {
                            errorToast(genericErrorMessage());
                        }
                    });
                }
            },
            error: function () {
                sloader.hide();
                errorToast("An error has occurred, please check the file size and try again later.");
            }
        });
    }
    function importToPostgres() {
        $("#inputImport").trigger('click');
    }
</script>